home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / mmdf / phost < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  8.7 KB  |  227 lines

  1. #!/bin/ksh
  2. # @(#) phost.ksh 1.3 96/06/13
  3. # 96/05/17 john h. dubois iii (john@armory.com)
  4. # 96/06/02 Allow mangling of list return address.
  5. # 96/06/13 Do not require malias and /bin/env to exist.
  6. # 97/06/09 Added u option.
  7.  
  8. # phost: convert user@host to user.host to implement per-hostname user aliases.
  9. # This program is intended to be executed for the phost channel by the
  10. # sendprog channel program.
  11. #
  12. # Portability notes:
  13. # The sendprog channel program must be available for this to work.
  14. #    sendprog is not shipped with SCO systems.  An archive containing
  15. #    sendprog for SCO 3.2v5 is available at
  16. #    ftp://ftp.armory.com/pub/scobins/progchan.tar.Z, and for SCO 3.2v4 at
  17. #    ftp://ftp.armory.com/pub/scobins/3.2v4/progchan.tar.Z  The 'sendprog'
  18. #    file in those archives should be put in the directory /usr/mmdf/chans
  19. # This is a ksh program.  It should also work as a pd-ksh, bash, or zsh
  20. #    program, but might require tweaking.
  21. #
  22. # Instructions for use:
  23. # 1. Put this program in /usr/mmdf/bin/phost and make it readable and
  24. #    executable.
  25. # 2. Create a queue directory for phost.  cd to /usr/spool/mmdf/lock/home and
  26. #    do:
  27. #    mkdir q.phost; chown mmdf q.phost; chgrp mmdf q.phost; chmod 777 q.phost
  28. #    Note: the queue directories will probably be somewhere other than
  29. #    /usr/spool/mmdf/lock/home on non-SCO systems.
  30. # 3. Add an mmdftailor entry like this:
  31. # MCHN    phost, show="pseudo-host channel", ap=822, pgm=sendprog, mod=imm,
  32. #    tbl=phost.chn,
  33. #    confstr="/usr/mmdf/bin/phost [opts] $(from) $(to.user) $(to.host) $(local)"
  34. #    where [opts] is any options you want to give to the phost program
  35. #    (currently, only the -u option is defined).
  36. #
  37. # 4. Put any hostnames for which this mapping should be done into the phost.chn
  38. # file.  No value needs to be associated with hostname in the table (just the
  39. # name alone on a line will do).  If the hostnames were previously in any other
  40. # channel table (e.g. local.chn if mail for a host was formerly accepted by
  41. # the local host without mapping), take them out of those tables.
  42. #
  43. # 4.1. Before the channel table lookup is done, a hostname must be found in
  44. # a domain table.  Therefore, if the hostnames you put in phost.chn are not
  45. # yet in any domain table, put them in an appropriate domain table, for
  46. # example, root.dom.  The right hand side of each entry in the domain table
  47. # should exactly match the entry in phost.chn.  A typical entry would go in
  48. # root.dom and simply map each name to itself.  If the MMDF system was
  49. # previously configured to accept mail locally for the pseudo-hosts, they
  50. # are already in a domain table, so this step is unneccessary.  Also, if the
  51. # hostnames have appropriate DNS records and MMDF is configured to do DNS
  52. # domain table lookups, that will suffice for the domain table lookup.
  53. # Once the table database is rebuilt, mail sent to a user at a host listed
  54. # in the phost.chn table will be re-submitted into the mail system with its
  55. # recipient address changed from user@host to user.host.  NOTE that due to the
  56. # way the sendprog channel works, this will be done separately for each
  57. # recipient address delivered via the phost channel, so a single submitted
  58. # message is liable to result in multiple messages being re-submitted to the
  59. # mail system.
  60. #
  61. # 5. An alias for any username that should be mailable at the alternate host
  62. # should be created.  An alias must be created for every user; the default
  63. # for recipient addresses that no alias exists for is to bounce the message,
  64. # not to deliver it to the recipient username at the local hostname (this
  65. # could be changed by modifying the script).
  66. # Example:
  67. # phost.chn contents:
  68. # alt.domain
  69. # alias table contents:
  70. # user1.alt.domain    foo
  71. # user2.alt.domain    bar@that.host
  72. # This will cause mail to user1@alt.domain to be sent to local user foo,
  73. # and mail to user2@alt.domain to be sent to bar@that.host.
  74. #
  75. # ALTERNATE steps 3 and 4, to allow for customized processing on a per-domain
  76. # basis:
  77. # 3. Add an mmdftailor entry like this:
  78. # MCHN    phost, show="pseudo-host channel", ap=822, pgm=sendprog, mod=imm,
  79. #    tbl=phost.chn
  80. # 4. Put any hostnames for which this mapping should be done into the phost.chn
  81. # file.  The righthand side of the table for each hostname should be:
  82. # /usr/mmdf/bin/phost [options] $(from) $(to.user) $(to.host) $(local)
  83. # Example: 
  84. #
  85. # alt.domain1 /usr/mmdf/bin/phost -u someone@somewhere $(from) $(to.user) $(to.host) $(local)
  86. # alt.domain2 /usr/mmdf/bin/phost $(from) $(to.user) $(to.host) $(local)
  87. #
  88. # If the hostnames were previously in any other channel table (e.g. local.chn
  89. # if mail for a host was formerly accepted by the local host without mapping),
  90. # take them out of those tables.
  91. #
  92. # Options:
  93. # phost has one option.  By default, mail sent to an unknown address is
  94. # bounced back to the return address.  If you give "-u<address>", all mail to
  95. # users who have no alias in the domain will be sent to <address>.  This option
  96. # depends on the existance of the "malias" program, which is expected to be
  97. # found in /usr/mmdf/bin/malias.  If it exists but is somewhere else, the
  98. # program should be tweaked accordingly.
  99.  
  100. # NOTE: there is a problem using this channel to create a mailing list name
  101. # within a domain if the mailing list is handled by the list channel.  The
  102. # list channel will always make the return address be listname-request, without
  103. # the alternate domain attached to it.
  104. # Short of modifying the list channel, about the best that can be done in this
  105. # situation is to simply implement the listname-request alias in the local
  106. # domain rather than the alternate domain.  If this would collide with another
  107. # list name, the outbound name passed to the list processor (e.g.
  108. # foo-outbound@list-processor) can be changed.  If the list-request alias 
  109. # should also exist in the alternate domain, as an administrative alias to be
  110. # mailed to, it should be separately aliased.
  111. # Example alias file entries to use this method:
  112. # foo.nonesuch.org:            foo-outbound@list-processor
  113. # foo-request:                 spcecdt
  114. # foo-request.nonesuch.org:    spcecdt
  115. # foo-outbound:                recipient1 recipient2 recipient3
  116.  
  117. # The alternative to all of this is to have this program itself handle
  118. # conversion of the list outbound address to a different name and set the
  119. # return address itself.  This program therefore recognizes a name ending
  120. # in '-mangle' and translates it in the same way as it does other messages,
  121. # but also sets the return address by replacing -mangle with
  122. # -request@alternate-host.
  123. # Example alias file entries to use this method:
  124. # foo.nonesuch.org:             foo-mangle@nonesuch.org
  125. # foo-request.nonesuch.org:     spcecdt
  126. # foo-mangle.nonesuch.org:      recipient1 recipient2 recipient3
  127.  
  128. ##############################
  129.  
  130. # If mail is e.g. sent from johnd@sco.com to root@nonesuch.org,
  131. # and the local machine name (MLNAME.MLDOMAIN) is armory.com,
  132. # these vars would be set as follows:
  133. # From: johnd@sco.com
  134. # ToUser: root
  135. # ToHost: nonesuch.org
  136. # Local: armory.com
  137.  
  138.  
  139. name=${0##*/}
  140. Usage=\
  141. "Usage: $name [-u<unknown-address>] from-address to-user to-host local-host"
  142.  
  143. while getopts u: opt; do
  144.     case $opt in
  145.     u)
  146.     unknownAddr=$OPTARG
  147.     ;;
  148.     esac
  149. done
  150.  
  151. # remove args that were options
  152. let OPTIND=OPTIND-1
  153. shift $OPTIND
  154.  
  155. if [ $# -ne 4 ]; then
  156.     print -r -u2 -- "$name: Wrong number of arguments (expected 4, got $#).
  157. $Usage"
  158.     exit 1
  159. fi
  160.  
  161. From=$1
  162. ToUser=$2
  163. ToHost=$3
  164. Local=$4
  165.  
  166. OrigDest="$ToUser@$ToHost"
  167. NewDest="$ToUser.$ToHost"
  168. admin=mmdf@$Local
  169.  
  170. if [[ "$ToUser" = *-mangle ]]; then
  171.     Return="${ToUser%mangle}request@$ToHost"
  172. else
  173.     Return=$From
  174. fi
  175.  
  176.  
  177. # If env is available, use it to hide any local user environment variables
  178. # from execmail, just in case
  179. if [ -x /bin/env ]; then
  180.     alias env=/bin/env
  181. else
  182.     function env {
  183.     # If there are actually specific env. vars found to cause problems,
  184.     # they could be unset here.
  185.     shift 2
  186.     "$@"
  187.     }
  188. fi
  189.  
  190. function bounce {
  191.     {
  192.     # Let execmail generate all of header except From: line
  193.     print -r -- \
  194. "From: $admin (MMDF Mail System)
  195.  
  196. Trouble sending mail on $Local:
  197.  
  198. ============ Transcript follows ============
  199.  
  200. Unknown user name: \"$OrigDest\"
  201. (\"$ToUser\" is not a user in the domain \"$ToHost\")
  202.  
  203. ============== Message follows =============
  204. "
  205.     /bin/cat
  206.     } | env - "TZ=$TZ" /usr/lib/mail/execmail -f "$admin" "$From"
  207.     exit 0
  208. }
  209.  
  210. # If the the malias program is available, use it to check whether the recipient
  211. # address is aliased in the given domain.  If not, bounce it here, because if
  212. # bounced by execmail it will tell the originator that there is no such user
  213. # "user.host" which is liable to be confusing.
  214.  
  215. malias=/usr/mmdf/bin/malias
  216. if [ -x $malias ]; then
  217.     set -- `/usr/mmdf/bin/malias "$NewDest"`
  218.     if [ "$1 $2" = "no aliases" ]; then
  219.     [ -n "$unknownAddr" ] && NewDest=$unknownAddr || bounce
  220.     fi
  221. fi
  222.  
  223. env - "TZ=$TZ" /usr/lib/mail/execmail -f "$Return" "$NewDest"
  224.  
  225. exit 0
  226.